package com.lin.ai.config;

import com.lin.ai.constants.DPAiConstants;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore.MetadataField;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPooled;

/**
 * <p>
 * ChatClientConfig
 * </p>
 *
 * @author liyang
 * @since 2025/4/22 14:34
 */
@Configuration
public class ChatClientConfig {

    @Bean
    public ChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }

    @Bean
    public ChatClient chatClient(OllamaChatModel model, ChatMemory chatMemory){
        return ChatClient
                .builder(model)
                .defaultSystem(DPAiConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory) // 添加一个聊天记忆拦截器
                        , new SimpleLoggerAdvisor()
                )
                .build();
    }
    @Bean
    public VectorStore vectorStore(JedisPooled jedis, EmbeddingModel embeddingClient) {
        return RedisVectorStore.builder(jedis, embeddingClient)
                .build();
    }


}